﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Drawing.PieChart;

namespace EventScavenger
{
    public partial class PieGraphView : Form
    {
        public PieGraphView()
        {
            InitializeComponent();
        }

        private Color[] availableColors =
            new Color[] { Color.Salmon, Color.LightGreen, Color.LightBlue, Color.Yellow, Color.Brown, Color.Cyan, Color.Orange };
        private int lastHighLightIndex = -1;
        private PrintDialog m_printDialog;
        private float initialAngle = 170f;

        #region ShowPieChart
        public void ShowPieChart(string title, string[] pieTexts, string[] pieTooltips, decimal[] pieValues, float[] pieDisplacements)
        {
            this.Text = title;
            pieChartControl1.LeftMargin = 10;
            pieChartControl1.RightMargin = 10;
            pieChartControl1.TopMargin = 10;
            pieChartControl1.BottomMargin = 10;
            pieChartControl1.FitChart = true;
            pieChartControl1.SliceRelativeHeight = 0.2f;
            pieChartControl1.InitialAngle = initialAngle;
            pieChartControl1.EdgeLineWidth = 1.5f;
            pieChartControl1.Values = pieValues;
            pieChartControl1.Texts = pieTexts;
            pieChartControl1.ToolTips = pieTooltips;
            pieChartControl1.SliceRelativeDisplacements = pieDisplacements;
            pieChartControl1.ForeColor = Color.FromArgb(250, Color.Black);
            pieChartControl1.EdgeColorType = System.Drawing.PieChart.EdgeColorType.EnhancedContrast;

            Color[] colors = new Color[pieTexts.Length];
            for (int i = 0; i < pieTexts.Length; i++)
            {
                colors[i] = Color.FromArgb(122, availableColors[i % availableColors.Length]);
            }
            pieChartControl1.Colors = colors;

            this.ShowDialog();
        } 
        #endregion

        #region Pie chart events
        private void pieChartControl1_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                if (lastHighLightIndex != pieChartControl1.LastHighLightIndex)
                {
                    lastHighLightIndex = pieChartControl1.LastHighLightIndex;

                    //collapse
                    if (lastHighLightIndex > -1)
                    {
                        if (pieChartControl1.SliceRelativeDisplacements[lastHighLightIndex] > 0f)
                        {
                            pieChartControl1.SliceRelativeDisplacements[lastHighLightIndex] = 0.1f;
                            pieChartControl1.Invalidate();
                            Application.DoEvents();
                        }
                    }

                    for (int i = 0; i < pieChartControl1.SliceRelativeDisplacements.Length; i++)
                    {
                        pieChartControl1.SliceRelativeDisplacements[i] = 0f;
                    }
                    pieChartControl1.Invalidate();
                    Application.DoEvents();

                    //expand
                    if (lastHighLightIndex > -1)
                    {
                        for (int i = 0; i < 4; i++)
                        {
                            pieChartControl1.SliceRelativeDisplacements[lastHighLightIndex] = 0.1f * i;
                            pieChartControl1.Invalidate();
                            Application.DoEvents();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        } 
        #endregion

        #region Print label
        private void label1_MouseEnter(object sender, EventArgs e)
        {
            label1.ForeColor = Color.Black;
        }
        private void label1_MouseLeave(object sender, EventArgs e)
        {
            label1.ForeColor = Color.DimGray;
        }
        private void label1_Click(object sender, EventArgs e)
        {
            m_printDialog = new PrintDialog();
            PrintDocument pd = new PrintDocument();
            m_printDialog.Document = pd;
            m_printDialog.UseEXDialog = true;
            m_printDialog.Document.DefaultPageSettings.Landscape = true;
            if (m_printDialog.ShowDialog(this) == DialogResult.OK)
            {
                pd.PrinterSettings = m_printDialog.PrinterSettings;

                PieChart3D pieChart = new PieChart3D(200, 100, 800, 600, pieChartControl1.Values, pieChartControl1.SliceRelativeHeight);// (float)numericUpDownPieHeight.Value);
                pieChart.Colors = pieChartControl1.Colors;
                pieChart.SliceRelativeDisplacements = pieChartControl1.SliceRelativeDisplacements;
                pieChart.InitialAngle = initialAngle;
                pieChart.Texts = pieChartControl1.Texts;
                pieChart.Font = new Font("Arial", 10F);
                pieChart.ForeColor = SystemColors.WindowText;
                PrintPieChart pc = new PrintPieChart(pieChart);
                pc.DoPrint(pd);
            }
        } 
        #endregion
    }
}
